package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"group-card/common/request"
	"group-card/common/tool"
	"group-card/model/entity"
	"group-card/rpc/card/cardclient"
	"group-card/rpc/card/internal/svc"
)

type TraceabilityUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTraceabilityUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TraceabilityUpdateLogic {
	return &TraceabilityUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TraceabilityUpdateLogic) TraceabilityUpdate(in *cardclient.TraceabilityUpdateRequest) (*cardclient.TraceabilityUpdateResponse, error) {
	data := entity.CardTraceabilityReq{
		Id:             in.Id,
		CardNumber:     in.CardNumber,
		StorageAdminId: in.StorageAdminId,
		StorageTime:    tool.StringToTime(in.StorageTime),
		PublishAdminId: in.PublishAdminId,
		PublishTime:    tool.StringToTime(in.PublishTime),
		ReceiveAdminId: in.ReceiveAdminId,
		ReceiveTime:    tool.StringToTime(in.ReceiveTime),
		BindingUserId:  in.BindingUserId,
		BindingTime:    tool.StringToTime(in.BindingTime),
		WriteOffTime:   tool.StringToTime(in.WriteOffTime),
		Status:         in.Status,
		BaseReq: request.BaseReq{
			Columns: "card_number,storage_admin_id,storage_time,publish_admin_id,publish_time,receive_admin_id,receive_time" +
				",binding_user_id,binding_time,write_off_time,status",
		},
	}

	var ctx context.Context
	err := l.svcCtx.TraceabilityModel.Update(ctx, &data)

	if err != nil {
		return nil, err
	}

	return &cardclient.TraceabilityUpdateResponse{}, nil
}
